﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using User_subsystem;
using web_search;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace BookManageSystem
{
    public partial class FormAddBooks : Form
    {
        public FormAddBooks()
        {
            InitializeComponent();
        }

        private bool checkbookid(String isbn,String bookid)
        {
            string[] res = bookid.Split('-');
            int len = res.Length;
            //bookid不是两段直接返回错
            if (len != 2)
            {
                return false;
            }

            if (res[0].Equals(isbn))
            {
                return true;
            }
            
            return false;
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textISBN.Text = textISBN.Text.Trim();
            textBookid.Text = textBookid.Text.Trim();
            textTitle.Text = textTitle.Text.Trim();
            textAuthor.Text = textAuthor.Text.Trim();
            textPrice.Text= textPrice.Text.Trim();
            //如果输入为空，则返回
            if (textISBN.Text.Trim() == "" ||
                textBookid.Text.Trim() == "" ||
                textTitle.Text.Trim() == "" ||
                textAuthor.Text.Trim() == "" ||
                textPrice.Text.Trim() == "" 
                )
            {
                //提示有空项 返回
                MessageBox.Show("有空项","消息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }

            //检查ISBN和BookID的关系是否符合
            if ( !  checkbookid(textISBN.Text, textBookid.Text)  )
            {
                MessageBox.Show("图书编号命名格式不正确！请检查后重新命名。","消息",MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            

            //添加图书到数据库
            DaoAdmin dao = new DaoAdmin();
            dao.connect();

            //查询该bookid是否已存在
            string check_id = $"select * from BookInfor where BookID= '{textBookid.Text}' ";
            SqlDataReader reader=dao.read(check_id);
            
            if (reader.Read()==false)
            {
                //不存在，可以插入
                float price = float.Parse(textPrice.Text);
                string sql = $"Insert into BookInfor values('{textBookid.Text}','{textTitle.Text}',{price},'{textISBN.Text}','{textAuthor.Text}')";
                dao.command(sql);
                
                if (dao.Execute(sql) > 0)
                {
                    MessageBox.Show("添加图书成功！","消息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show("添加图书失败！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                //存在这本书，提示管理员重新设置图书编号
                MessageBox.Show("该图书编号已存在，请检查后重新设置图书编号。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }


        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void 取消_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textISBN_TextChanged(object sender, EventArgs e)
        {

        }

        private void FormAddBooks_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
